<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <title>制定入库单</title>

    <link rel="stylesheet" href="../css/pintuer.css">
    <link rel="stylesheet" href="../css/admin.css">
    <link rel="stylesheet" href="../css/bootstrap.css">

    <script src="../js/jquery-3.4.1.min.js"></script>
    <script src="../js/bootstrap-table1.15.3.js"></script>
    <script src="../js/bootstrap-table-zh-CN.min.js"></script>
    <script src="../js/popper.min.js"></script>
    <script src="../js/bootstrap.min.js"></script>

    <style>
        input{
            border-top: none;
            border-left: none;
            border-right: none;
            border-bottom: black solid 1px;
        }
        table{
            border: 1px;
            cell-padding:0;
            cell-spacing:0;
            width: 80%;
            margin-top: 20px;
        }
        table thead{
            background-color:#b3d7ff ;
        }
    </style>

</head>

<script>

    //父级ID
    var parentId;
    //最大库存容量
    var maxCapacityAmount;
    //应该入库件数
    var amount;

    function initOutSetSelect(){
        $('#outSetSelectTableInfo').bootstrapTable(
            {
                //请求方式
                method:'post',
                contentType:'application/json',
                url:'/sPay/queryOutSetAll.action',
                //设置交替样式
                striped:true,
                //初始显示页号
                pageNumber:1,
                //是否显示分页
                pagination:true,
                //是否设置服务端分页还是客户端分页
                sidePagination:'server',
                //页面大小
                pageSize:3,
                //分页列表
                pageList:[1,3,5,6,10],
                //上传到到服务端的参数
                queryParams:function (pares) {
                    return{
                        //计算页号（当前页）
                        pageNum:(pares.offset/pares.limit)+1,
                        //计算页大小
                        pageSize:pares.limit,
                    }
                },
                //第一列名
                columns:[
                    {title:"出库单编号",field:"payId",align:"center"},
                    {title:"出库理由",field:"reason",align:"center",formatter:function (value,row,index) {
                            if(value=="R001-1" || value=="C001-1"){
                                return "生产领料";
                            }else if(value=="R001-2" || value=="C001-2"){
                                return "赠送";
                            }else if(value=="R001-3" || value=="C001-3"){
                                return "内部借领";
                            }else if(value=="R001-4" || value=="C001-4"){
                                return "其他借领";
                            }
                        }},
                    {title:"登记时间",field:"registerTime",align:"center"},
                    {title:"总件数",field:"amountSum",align:"center"},
                    {title:"总金额",field:"costPriceSum",align:"center"},
                    {title:"审核",align:"center",formatter:function (value,row,index) {
                            return "<a href='#' onclick='updateRegister("+JSON.stringify(row)+")'>出库调度</a>"
                        } }
                ]
            })
    }

    $(function () {
        initOutSetSelect();

        //出库调度
        $('#outSubmitBtn').click(function () {
            //本次入库数量
            let gatheredAmount = $('#gatheredAmount').val();
            //产品编号
            let productId = $('#productId').val();
            //应入库的数量
            let amount = $('#amount').val();
            //当前可存放数量
            // let sCellAmount = $('#sCellAmountTd').text();
            //登记时间
            let registerTime = $('#registerTime1').val();
            if(maxCapacityAmount<gatheredAmount){
                alert('本次加入库存的数量，已超过可容纳的库存量')
            }else if(amount != gatheredAmount){
                alert('您输入的库存数量不符合')
            }else{
                $.ajax({
                    type:'post',
                    url:'/sPayDetails/updateSPayDetailsByProductId1',
                    data:'gatheredAmount='+gatheredAmount+'&productId='+productId+
                        '&registerTime='+registerTime+'&parentId='+parentId,
                    success:function () {
                        alert('调度成功')
                        $('#outSetSubmitModal').modal('hide');
                        $('#'+resId).text('已调度')
                        $('#outSetSelectTableInfo').bootstrapTable('refresh')
                    },
                    error:function () {
                        alert('调度失败')
                    }
                })
            }
        })

    })

        //确认审核  制定出库单审核
        $('#submitBtn').click(function () {
            let formValue = $('#updateSPayFormId').serialize();
            /*alert(formValue)
            alert($('#payId').val())*/
            $.ajax({
                type:'post',
                url:'/sPay/updateSPayByPayId',
                data:formValue,
                success:function (result) {
                    if(result=='S001-1'){
                        alert('审核通过')
                    }else if(result=='S001-2'){
                        alert('审核不通过')
                    }
                    window.location='outRegisterCheck.html'
                },
                error:function () {
                    alert('数据提交失败')
                }
            })

        });

    //将数据添加到 “出库申请单” 的表格中
    function updateRegister(rows) {
        // alert(rows.productId)
        // var flag=false;
        $.ajax({
            type:'post',
            url:'/sCell/querySCellByProduct.action',
            data:'productId='+rows.productId,
            success:function (result) {
                if(result!=0){
                    // flag=true
                    $('#outSetModal').modal('show')
                    $('#payId').val(rows.payId);
                    $('#storer').val(rows.storer);
                    let reason = rows.reason;
                    if(reason=='R001-1'){
                        reason='生产出库';
                    }else if(reason=='R001-2'){
                        reason='库存初始';
                    }else if(reason=='R001-3'){
                        reason='赠送';
                    }else if(reason=='R001-4'){
                        reason='内部借领';
                    }else if(reason=='R001-5'){
                        reason='其他借领';
                    }

                    parentId=rows.id;

                    $('#reason').val(reason);
                    $('#amountSum').val(rows.amountSum);
                    $('#paidAmountSum').val(rows.paidAmountSum);

                    $('#tbodyId').empty();
                    $.ajax({
                        type:'post',
                        url:'/sPayDetails/querySPayDetailsAll.action',
                        data:'parentId='+rows.id,
                        success:function (result) {
                            let tbodyId = $('#tbodyId');
                            $.each(result,function (i,res) {
                                let trObj = $('<tr>');
                                tbodyId.append(trObj);
                                trObj.append($('<td>').text(i+1));
                                trObj.append($('<td>').text(res.productName));
                                trObj.append($('<td>').text(res.productId));
                                //数量
                                trObj.append($('<td class="amountTd">').text(res.amount));
                                // trObj.append($('<td>').text(res.paidAmount));
                                if(res.payTag=='K002-3'){
                                    trObj.append($('<td>').text('已调度'))
                                }else {
                                    trObj.append($('<td id="'+res.id+'">').html("<a href='#' onclick='updateSPayDetails("+JSON.stringify(res)+")' data-toggle='modal' data-target='#outSetSubmitModal'>调度</a>"));
                                }
                            })
                        }
                    });
                }else {
                    alert('该产品还没有入库，请先进行入库')
                }
            }
        })

    }
    var resId;
    function updateSPayDetails(res) {
        resId=res.id;
        $('#payId1').val($('#payId').val());
        $('#productId').val(res.productId);
        //登记时间
        var today=new Date();
        let year = today.getFullYear();
        let month = today.getMonth();
        let date = today.getDate();
        let hours = today.getHours();
        let minutes = today.getMinutes();
        let seconds = today.getSeconds();
        today=year+"-"+(month+1)+"-"+date+" "+hours+":"+minutes+":"+seconds;
        //登记时间
        $('#registerTime1').val(today);

        $('#tbodyId1').empty();
        $.ajax({
            type:'post',
            url:'/sPayDetails/querySPayDetailsByParentIdByProductId.action',
            data:'parentId='+parentId+'&productId='+res.productId,
            success:function (result) {
                $('#productName').val(result.productName)
                $('#costPrice').text(result.costPrice)
                var tbodyIdObj = $('#tbodyId1')
                var trObj = $('<tr>')
                tbodyIdObj.append(trObj)
                trObj.append($('<td>').text(1))
                trObj.append($('<td id="sCellAmountTd">').text(result.sCell.amount))
                trObj.append($('<td>').html('<input size="5px" id="gatheredAmount">'))

                //登记人
                $('#register1').val(result.sPay.register)
                //应出库
                $('#amount').val(res.amount)
                //应出库成本
                $('#paidAmount').val(result.paidAmount)

                //获取最大库存容量
                maxCapacityAmount=result.sCell.maxCapacityAmount;
            }
        })
    }

</script>


<body bgcolor="#f5f5f5">

<div class="container-fluid">
    <div style="width: 100%;margin: 0px;padding: 0px;">
        <table id="outSetSelectTableInfo" class="table table-bordered table-hover table-striped table-condensed">

        </table>
    </div>
</div>

<!--模态框-->
<!--modal fade-->
<div class="modal  fade" id="outSetModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <form id="updateSPayFormId">
        <div class="modal-dialog" style="width: 90%">
            <div class="modal-content" style="border: 1px solid black;">
                <div class="modal-header">
                    <div class="container">
                        <div class="row">
                            <div class="col-7" style="text-align: right">
                                <h5 class="modal-title" id="info" >
                                    出库调度单
                                </h5>
                            </div>
                        </div>
                    </div>
                    <button type="button" class="close" data-dismiss="modal" aria-hidden="true" style="margin-right: 0px">

                    </button>
                </div>
                <div class="modal-body">
                    <div class="container-fluid">
                        <div class="row" style="margin-top: 10px">
                            <div class="col-12">
                                <div class="container-fluid">
                                    <div class="row">
                                        <div class="col-4">
                                            出库单编号:
                                            <input name="payId" id="payId"/>
                                        </div>
                                    </div>
                                    <div class="row" style="margin-top: 30px;">
                                        <div class="col-4">
                                            出库人：<input name="storer" id="storer">
                                        </div>

                                        <div class="col-4 offset-2">
                                            出库理由：<input id="reason" id="reason">
                                        </div>
                                    </div>

                                    <div>
                                        <table border="1" cellpadding="0" cellspacing="0" width="80%" style="margin-top: 20px;" class="table table-sm">
                                            <thead  style="background-color: #b3d7ff;">
                                            <tr>
                                                <th>序号</th>
                                                <th>产品名称</th>
                                                <th>产品编号</th>
                                                <th>应出库件数</th>
<!--                                                <th>已出库件数</th>-->
<!--                                                <th>存放地址集合</th>-->
                                                <th>调度</th>
                                            </tr>
                                            </thead>
                                            <tbody id="tbodyId">

                                            </tbody>
                                        </table>
                                    </div>

                                    <div class="row" style="margin-top: 30px;">
                                        <div class="col-4">
                                            应出库总件数：<input name="amountSum" id="amountSum">
                                        </div>
                                        <div class="col-4 offset-2">
<!--                                            已出库总件数：<input name="paidAmountSum" id="paidAmountSum">-->
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div><!-- /.modal-content -->
        </div><!-- /.modal -->
    </form>
</div>

<!--模态框-->
<!--modal fade-->
<div class="modal  fade" id="outSetSubmitModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog" style="width: 90%">
        <div class="modal-content" style="border: 1px solid black;">
            <div class="modal-header">
                <div class="container">
                    <div class="row">
                        <div class="col-7" style="text-align: right">
                            <h5 class="modal-title">
                                出库调度单
                            </h5>
                        </div>
                    </div>
                </div>
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true" style="margin-right: 0px"></button>
            </div>
            <div class="modal-body">
                <div class="container-fluid">
                    <div class="row" style="margin-top: 10px">
                        <div class="col-12">
                            <div class="container-fluid">
                                <div class="row">
                                    <div class="col-4">
                                        出库单编号:<input name="payId" id="payId1"/>
                                    </div>
                                    <div class="col-4 offset-2">
                                        产品编号：<input name="productId" id="productId">
                                    </div>
                                </div>
                                <div class="row" style="margin-top: 30px;">
                                    <div class="col-4">
                                        产品名称：<input name="productName" id="productName">
                                    </div>
                                    <div class="col-4 offset-2">
                                        成本单价：<span id="costPrice"></span>
                                    </div>
                                </div>

                                <div>
                                    <table border="1" cellpadding="0" cellspacing="0" width="80%" style="margin-top: 20px;" class="table table-sm">
                                        <thead  style="background-color: #b3d7ff;">
                                        <tr>
                                            <th>序号</th>
                                            <!--<th>库房名称</th>
                                            <th>存储地址编号</th>
                                            <th>存储地址名称</th>
                                            <th>存储单元简称</th>-->
                                            <th>当前可存放数量</th>
                                            <th>本次出库数量</th>
                                        </tr>
                                        </thead>
                                        <tbody id="tbodyId1">

                                        </tbody>
                                    </table>
                                </div>

                                <div class="row" style="margin-top: 30px;">
                                    <div class="col-4">
                                        应出库：<input name="amount" id="amount">
                                    </div>
                                    <div class="col-4 offset-2">
<!--                                        已出库数：<input name="paidAmount" id="paidAmount">-->
                                    </div>
                                </div>

                                <div class="row" style="margin-top: 30px;">
                                    <div class="col-4">
                                        登记人：<input name="register" id="register1">
                                    </div>
                                    <div class="col-4 offset-2">
                                        登记时间：<input name="registerTime" id="registerTime1">
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-primary" id="outSubmitBtn">
                    确认调度
                </button>
            </div>
        </div><!-- /.modal-content -->
    </div><!-- /.modal -->
</div>

</body>
</html>